<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>SQL练习 | Zhang Shuo'blog</title><meta name="keywords" content="SQL练习"><meta name="author" content="Zhang Shuo"><meta name="copyright" content="Zhang Shuo"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="SQL 练习  SQL 练习 595. Big Countries 627. Swap Salary 620. Not Boring Movies 596. Classes More Than 5 Students 182. Duplicate Emails 196. Delete Duplicate Emails 175. Combine Two Tables 181. Employees Ea">
<meta property="og:type" content="article">
<meta property="og:title" content="SQL练习">
<meta property="og:url" content="https://zhang-shuo-fr.gitee.io/hexo3/2021/12/06/notes/SQL%20%E7%BB%83%E4%B9%A0/index.html">
<meta property="og:site_name" content="Zhang Shuo&#39;blog">
<meta property="og:description" content="SQL 练习  SQL 练习 595. Big Countries 627. Swap Salary 620. Not Boring Movies 596. Classes More Than 5 Students 182. Duplicate Emails 196. Delete Duplicate Emails 175. Combine Two Tables 181. Employees Ea">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://zhang-shuo-fr.gitee.io/hexo3/img/13.jpg">
<meta property="article:published_time" content="2021-12-06T09:36:00.000Z">
<meta property="article:modified_time" content="2021-12-10T13:10:27.611Z">
<meta property="article:author" content="Zhang Shuo">
<meta property="article:tag" content="SQL练习">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://zhang-shuo-fr.gitee.io/hexo3/img/13.jpg"><link rel="shortcut icon" href="/hexo3/img/mao_tou_xiang.jpg"><link rel="canonical" href="https://zhang-shuo-fr.gitee.io/hexo3/2021/12/06/notes/SQL%20%E7%BB%83%E4%B9%A0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="manifest" href="/hexo3/pwa/manifest.json"/><link rel="apple-touch-icon" sizes="180x180" href="/hexo3/pwa/apple-touch-icon.png"/><link rel="icon" type="image/png" sizes="32x32" href="/hexo3/pwa/32.png"/><link rel="icon" type="image/png" sizes="16x16" href="/hexo3/pwa/16.png"/><link rel="mask-icon" href="/hexo3/pwa/safari-pinned-tab.svg" color="#5bbad5"/><link rel="stylesheet" href="/hexo3/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/hexo3/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'SQL练习',
  isPost: true,
  isHome: false,
  isHighlightShrink: undefined,
  isToc: true,
  postUpdate: '2021-12-10 21:10:27'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    document.addEventListener('pjax:complete', detectApple)})(window)</script><style type="text/css">#toggle-sidebar {left:100px}</style><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/hexo3/img/mao_tou_xiang.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/hexo3/archives/"><div class="headline">文章</div><div class="length-num">176</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/hexo3/tags/"><div class="headline">标签</div><div class="length-num">45</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/hexo3/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hexo3/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/hexo3/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/hexo3/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/hexo3/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 小窝</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hexo3/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/hexo3/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/hexo3/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/hexo3/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/hexo3/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/hexo3/img/13.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/hexo3/">Zhang Shuo'blog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/hexo3/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/hexo3/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/hexo3/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/hexo3/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 小窝</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/hexo3/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/hexo3/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/hexo3/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/hexo3/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/hexo3/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">SQL练习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-12-06T09:36:00.000Z" title="发表于 2021-12-06 17:36:00">2021-12-06</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-12-10T13:10:27.611Z" title="更新于 2021-12-10 21:10:27">2021-12-10</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/hexo3/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>16分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="SQL练习"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="SQL-练习"><a href="#SQL-练习" class="headerlink" title="SQL 练习"></a>SQL 练习</h1><!-- GFM-TOC -->
<ul>
<li><a href="#sql-%E7%BB%83%E4%B9%A0">SQL 练习</a><ul>
<li><a href="#595-big-countries">595. Big Countries</a></li>
<li><a href="#627-swap-salary">627. Swap Salary</a></li>
<li><a href="#620-not-boring-movies">620. Not Boring Movies</a></li>
<li><a href="#596-classes-more-than-5-students">596. Classes More Than 5 Students</a></li>
<li><a href="#182-duplicate-emails">182. Duplicate Emails</a></li>
<li><a href="#196-delete-duplicate-emails">196. Delete Duplicate Emails</a></li>
<li><a href="#175-combine-two-tables">175. Combine Two Tables</a></li>
<li><a href="#181-employees-earning-more-than-their-managers">181. Employees Earning More Than Their Managers</a></li>
<li><a href="#183-customers-who-never-order">183. Customers Who Never Order</a></li>
<li><a href="#184-department-highest-salary">184. Department Highest Salary</a></li>
<li><a href="#176-second-highest-salary">176. Second Highest Salary</a></li>
<li><a href="#177-nth-highest-salary">177. Nth Highest Salary</a></li>
<li><a href="#178-rank-scores">178. Rank Scores</a></li>
<li><a href="#180-consecutive-numbers">180. Consecutive Numbers</a></li>
<li><a href="#626-exchange-seats">626. Exchange Seats</a><!-- GFM-TOC --></li>
</ul>
</li>
</ul>
<h2 id="595-Big-Countries"><a href="#595-Big-Countries" class="headerlink" title="595. Big Countries"></a>595. Big Countries</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/big-countries/description/">https://leetcode.com/problems/big-countries/description/</a></p>
<h3 id="Description"><a href="#Description" class="headerlink" title="Description"></a>Description</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+-----------------+------------+------------+--------------+---------------+</span><br><span class="line">| name            | continent  | area       | population   | gdp           |</span><br><span class="line">+-----------------+------------+------------+--------------+---------------+</span><br><span class="line">| Afghanistan     | Asia       | 652230     | 25500100     | 20343000      |</span><br><span class="line">| Albania         | Europe     | 28748      | 2831741      | 12960000      |</span><br><span class="line">| Algeria         | Africa     | 2381741    | 37100000     | 188681000     |</span><br><span class="line">| Andorra         | Europe     | 468        | 78115        | 3712000       |</span><br><span class="line">| Angola          | Africa     | 1246700    | 20609294     | 100990000     |</span><br><span class="line">+-----------------+------------+------------+--------------+---------------+</span><br></pre></td></tr></table></figure>

<p>查找面积超过 3,000,000 或者人口数超过 25,000,000 的国家。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+--------------+-------------+--------------+</span><br><span class="line">| name         | population  | area         |</span><br><span class="line">+--------------+-------------+--------------+</span><br><span class="line">| Afghanistan  | 25500100    | 652230       |</span><br><span class="line">| Algeria      | 37100000    | 2381741      |</span><br><span class="line">+--------------+-------------+--------------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution"><a href="#Solution" class="headerlink" title="Solution"></a>Solution</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> name,</span><br><span class="line">    population,</span><br><span class="line">    area</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    World</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    area <span class="operator">&gt;</span> <span class="number">3000000</span></span><br><span class="line">    <span class="keyword">OR</span> population <span class="operator">&gt;</span> <span class="number">25000000</span>;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema"><a href="#SQL-Schema" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><p>SQL Schema 用于在本地环境下创建表结构并导入数据，从而方便在本地环境调试。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> World;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> World ( NAME <span class="type">VARCHAR</span> ( <span class="number">255</span> ), continent <span class="type">VARCHAR</span> ( <span class="number">255</span> ), area <span class="type">INT</span>, population <span class="type">INT</span>, gdp <span class="type">INT</span> );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> World ( NAME, continent, area, population, gdp )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="string">&#x27;Afghanistan&#x27;</span>, <span class="string">&#x27;Asia&#x27;</span>, <span class="string">&#x27;652230&#x27;</span>, <span class="string">&#x27;25500100&#x27;</span>, <span class="string">&#x27;203430000&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;Albania&#x27;</span>, <span class="string">&#x27;Europe&#x27;</span>, <span class="string">&#x27;28748&#x27;</span>, <span class="string">&#x27;2831741&#x27;</span>, <span class="string">&#x27;129600000&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;Algeria&#x27;</span>, <span class="string">&#x27;Africa&#x27;</span>, <span class="string">&#x27;2381741&#x27;</span>, <span class="string">&#x27;37100000&#x27;</span>, <span class="string">&#x27;1886810000&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;Andorra&#x27;</span>, <span class="string">&#x27;Europe&#x27;</span>, <span class="string">&#x27;468&#x27;</span>, <span class="string">&#x27;78115&#x27;</span>, <span class="string">&#x27;37120000&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;Angola&#x27;</span>, <span class="string">&#x27;Africa&#x27;</span>, <span class="string">&#x27;1246700&#x27;</span>, <span class="string">&#x27;20609294&#x27;</span>, <span class="string">&#x27;1009900000&#x27;</span> );</span><br></pre></td></tr></table></figure>

<h2 id="627-Swap-Salary"><a href="#627-Swap-Salary" class="headerlink" title="627. Swap Salary"></a>627. Swap Salary</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/swap-salary/description/">https://leetcode.com/problems/swap-salary/description/</a></p>
<h3 id="Description-1"><a href="#Description-1" class="headerlink" title="Description"></a>Description</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line">| id | name | sex | salary |</span><br><span class="line">|----|------|-----|--------|</span><br><span class="line">| 1  | A    | m   | 2500   |</span><br><span class="line">| 2  | B    | f   | 1500   |</span><br><span class="line">| 3  | C    | m   | 5500   |</span><br><span class="line">| 4  | D    | f   | 500    |</span><br></pre></td></tr></table></figure>

<p>只用一个 SQL 查询，将 sex 字段反转。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">| id | name | sex | salary |</span><br><span class="line">|----|------|-----|--------|</span><br><span class="line">| 1  | A    | f   | 2500   |</span><br><span class="line">| 2  | B    | m   | 1500   |</span><br><span class="line">| 3  | C    | f   | 5500   |</span><br><span class="line">| 4  | D    | m   | 500    |</span><br></pre></td></tr></table></figure>

<h3 id="Solution-1"><a href="#Solution-1" class="headerlink" title="Solution"></a>Solution</h3><p>两个相等的数异或的结果为 0，而 0 与任何一个数异或的结果为这个数。</p>
<p>sex  字段只有两个取值：’f’ 和 ‘m’，并且有以下规律：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&#x27;f&#x27; ^ (&#x27;m&#x27; ^ &#x27;f&#x27;) = &#x27;m&#x27; ^ (&#x27;f&#x27; ^ &#x27;f&#x27;) = &#x27;m&#x27;</span><br><span class="line">&#x27;m&#x27; ^ (&#x27;m&#x27; ^ &#x27;f&#x27;) = &#x27;f&#x27; ^ (&#x27;m&#x27; ^ &#x27;m&#x27;) = &#x27;f&#x27;</span><br></pre></td></tr></table></figure>

<p>因此将 sex 字段和 ‘m’ ^ ‘f’ 进行异或操作，最后就能反转 sex 字段。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">UPDATE salary</span><br><span class="line"><span class="keyword">SET</span> sex <span class="operator">=</span> <span class="type">CHAR</span> ( ASCII(sex) <span class="operator">^</span> ASCII( <span class="string">&#x27;m&#x27;</span> ) <span class="operator">^</span> ASCII( <span class="string">&#x27;f&#x27;</span> ) );</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-1"><a href="#SQL-Schema-1" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> salary;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> salary ( id <span class="type">INT</span>, NAME <span class="type">VARCHAR</span> ( <span class="number">100</span> ), sex <span class="type">CHAR</span> ( <span class="number">1</span> ), salary <span class="type">INT</span> );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> salary ( id, NAME, sex, salary )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="string">&#x27;2500&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;f&#x27;</span>, <span class="string">&#x27;1500&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;C&#x27;</span>, <span class="string">&#x27;m&#x27;</span>, <span class="string">&#x27;5500&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;D&#x27;</span>, <span class="string">&#x27;f&#x27;</span>, <span class="string">&#x27;500&#x27;</span> );</span><br></pre></td></tr></table></figure>

<h2 id="620-Not-Boring-Movies"><a href="#620-Not-Boring-Movies" class="headerlink" title="620. Not Boring Movies"></a>620. Not Boring Movies</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/not-boring-movies/description/">https://leetcode.com/problems/not-boring-movies/description/</a></p>
<h3 id="Description-2"><a href="#Description-2" class="headerlink" title="Description"></a>Description</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+---------+-----------+--------------+-----------+</span><br><span class="line">|   id    | movie     |  description |  rating   |</span><br><span class="line">+---------+-----------+--------------+-----------+</span><br><span class="line">|   1     | War       |   great 3D   |   8.9     |</span><br><span class="line">|   2     | Science   |   fiction    |   8.5     |</span><br><span class="line">|   3     | irish     |   boring     |   6.2     |</span><br><span class="line">|   4     | Ice song  |   Fantacy    |   8.6     |</span><br><span class="line">|   5     | House card|   Interesting|   9.1     |</span><br><span class="line">+---------+-----------+--------------+-----------+</span><br></pre></td></tr></table></figure>

<p>查找 id 为奇数，并且 description 不是 boring 的电影，按 rating 降序。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+---------+-----------+--------------+-----------+</span><br><span class="line">|   id    | movie     |  description |  rating   |</span><br><span class="line">+---------+-----------+--------------+-----------+</span><br><span class="line">|   5     | House card|   Interesting|   9.1     |</span><br><span class="line">|   1     | War       |   great 3D   |   8.9     |</span><br><span class="line">+---------+-----------+--------------+-----------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution-2"><a href="#Solution-2" class="headerlink" title="Solution"></a>Solution</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    <span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    cinema</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    id <span class="operator">%</span> <span class="number">2</span> <span class="operator">=</span> <span class="number">1</span></span><br><span class="line">    <span class="keyword">AND</span> description <span class="operator">!=</span> <span class="string">&#x27;boring&#x27;</span></span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span></span><br><span class="line">    rating <span class="keyword">DESC</span>;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-2"><a href="#SQL-Schema-2" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> cinema;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> cinema ( id <span class="type">INT</span>, movie <span class="type">VARCHAR</span> ( <span class="number">255</span> ), description <span class="type">VARCHAR</span> ( <span class="number">255</span> ), rating <span class="type">FLOAT</span> ( <span class="number">2</span>, <span class="number">1</span> ) );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> cinema ( id, movie, description, rating )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="string">&#x27;War&#x27;</span>, <span class="string">&#x27;great 3D&#x27;</span>, <span class="number">8.9</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="string">&#x27;Science&#x27;</span>, <span class="string">&#x27;fiction&#x27;</span>, <span class="number">8.5</span> ),</span><br><span class="line">    ( <span class="number">3</span>, <span class="string">&#x27;irish&#x27;</span>, <span class="string">&#x27;boring&#x27;</span>, <span class="number">6.2</span> ),</span><br><span class="line">    ( <span class="number">4</span>, <span class="string">&#x27;Ice song&#x27;</span>, <span class="string">&#x27;Fantacy&#x27;</span>, <span class="number">8.6</span> ),</span><br><span class="line">    ( <span class="number">5</span>, <span class="string">&#x27;House card&#x27;</span>, <span class="string">&#x27;Interesting&#x27;</span>, <span class="number">9.1</span> );</span><br></pre></td></tr></table></figure>

<h2 id="596-Classes-More-Than-5-Students"><a href="#596-Classes-More-Than-5-Students" class="headerlink" title="596. Classes More Than 5 Students"></a>596. Classes More Than 5 Students</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/classes-more-than-5-students/description/">https://leetcode.com/problems/classes-more-than-5-students/description/</a></p>
<h3 id="Description-3"><a href="#Description-3" class="headerlink" title="Description"></a>Description</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+---------+------------+</span><br><span class="line">| student | class      |</span><br><span class="line">+---------+------------+</span><br><span class="line">| A       | Math       |</span><br><span class="line">| B       | English    |</span><br><span class="line">| C       | Math       |</span><br><span class="line">| D       | Biology    |</span><br><span class="line">| E       | Math       |</span><br><span class="line">| F       | Computer   |</span><br><span class="line">| G       | Math       |</span><br><span class="line">| H       | Math       |</span><br><span class="line">| I       | Math       |</span><br><span class="line">+---------+------------+</span><br></pre></td></tr></table></figure>

<p>查找有五名及以上 student 的 class。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+---------+</span><br><span class="line">| class   |</span><br><span class="line">+---------+</span><br><span class="line">| Math    |</span><br><span class="line">+---------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution-3"><a href="#Solution-3" class="headerlink" title="Solution"></a>Solution</h3><p>对 class 列进行分组之后，再使用 count 汇总函数统计每个分组的记录个数，之后使用 HAVING 进行筛选。HAVING  针对分组进行筛选，而 WHERE 针对每个记录（行）进行筛选。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    class</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    courses</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">    class</span><br><span class="line"><span class="keyword">HAVING</span></span><br><span class="line">    <span class="built_in">count</span>( <span class="keyword">DISTINCT</span> student ) <span class="operator">&gt;=</span> <span class="number">5</span>;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-3"><a href="#SQL-Schema-3" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> courses;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> courses ( student <span class="type">VARCHAR</span> ( <span class="number">255</span> ), class <span class="type">VARCHAR</span> ( <span class="number">255</span> ) );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> courses ( student, class )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="string">&#x27;A&#x27;</span>, <span class="string">&#x27;Math&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;B&#x27;</span>, <span class="string">&#x27;English&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;C&#x27;</span>, <span class="string">&#x27;Math&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;D&#x27;</span>, <span class="string">&#x27;Biology&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;E&#x27;</span>, <span class="string">&#x27;Math&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;F&#x27;</span>, <span class="string">&#x27;Computer&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;G&#x27;</span>, <span class="string">&#x27;Math&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;H&#x27;</span>, <span class="string">&#x27;Math&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;I&#x27;</span>, <span class="string">&#x27;Math&#x27;</span> );</span><br></pre></td></tr></table></figure>

<h2 id="182-Duplicate-Emails"><a href="#182-Duplicate-Emails" class="headerlink" title="182. Duplicate Emails"></a>182. Duplicate Emails</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/duplicate-emails/description/">https://leetcode.com/problems/duplicate-emails/description/</a></p>
<h3 id="Description-4"><a href="#Description-4" class="headerlink" title="Description"></a>Description</h3><p>邮件地址表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+---------+</span><br><span class="line">| Id | Email   |</span><br><span class="line">+----+---------+</span><br><span class="line">| 1  | a@b.com |</span><br><span class="line">| 2  | c@d.com |</span><br><span class="line">| 3  | a@b.com |</span><br><span class="line">+----+---------+</span><br></pre></td></tr></table></figure>

<p>查找重复的邮件地址：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+---------+</span><br><span class="line">| Email   |</span><br><span class="line">+---------+</span><br><span class="line">| a@b.com |</span><br><span class="line">+---------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution-4"><a href="#Solution-4" class="headerlink" title="Solution"></a>Solution</h3><p>对 Email 进行分组，如果并使用 COUNT 进行计数统计，结果大于等于 2 的表示 Email  重复。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    Email</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Person</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">    Email</span><br><span class="line"><span class="keyword">HAVING</span></span><br><span class="line">    <span class="built_in">COUNT</span>( <span class="operator">*</span> ) <span class="operator">&gt;=</span> <span class="number">2</span>;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-4"><a href="#SQL-Schema-4" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> Person;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Person ( Id <span class="type">INT</span>, Email <span class="type">VARCHAR</span> ( <span class="number">255</span> ) );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Person ( Id, Email )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="string">&#x27;a@b.com&#x27;</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="string">&#x27;c@d.com&#x27;</span> ),</span><br><span class="line">    ( <span class="number">3</span>, <span class="string">&#x27;a@b.com&#x27;</span> );</span><br></pre></td></tr></table></figure>


<h2 id="196-Delete-Duplicate-Emails"><a href="#196-Delete-Duplicate-Emails" class="headerlink" title="196. Delete Duplicate Emails"></a>196. Delete Duplicate Emails</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/delete-duplicate-emails/description/">https://leetcode.com/problems/delete-duplicate-emails/description/</a></p>
<h3 id="Description-5"><a href="#Description-5" class="headerlink" title="Description"></a>Description</h3><p>邮件地址表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+---------+</span><br><span class="line">| Id | Email   |</span><br><span class="line">+----+---------+</span><br><span class="line">| 1  | john@example.com |</span><br><span class="line">| 2  | bob@example.com |</span><br><span class="line">| 3  | john@example.com |</span><br><span class="line">+----+---------+</span><br></pre></td></tr></table></figure>

<p>删除重复的邮件地址：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+------------------+</span><br><span class="line">| Id | Email            |</span><br><span class="line">+----+------------------+</span><br><span class="line">| 1  | john@example.com |</span><br><span class="line">| 2  | bob@example.com  |</span><br><span class="line">+----+------------------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution-5"><a href="#Solution-5" class="headerlink" title="Solution"></a>Solution</h3><p>只保留相同 Email 中 Id 最小的那一个，然后删除其它的。</p>
<p>连接查询：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DELETE</span> p1</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Person p1,</span><br><span class="line">    Person p2</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    p1.Email <span class="operator">=</span> p2.Email</span><br><span class="line">    <span class="keyword">AND</span> p1.Id <span class="operator">&gt;</span> p2.Id</span><br></pre></td></tr></table></figure>

<p>子查询：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DELETE</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Person</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    id <span class="keyword">NOT</span> <span class="keyword">IN</span> (</span><br><span class="line">        <span class="keyword">SELECT</span> id </span><br><span class="line">        <span class="keyword">FROM</span> ( </span><br><span class="line">            <span class="keyword">SELECT</span> <span class="built_in">min</span>( id ) <span class="keyword">AS</span> id </span><br><span class="line">            <span class="keyword">FROM</span> Person</span><br><span class="line">            <span class="keyword">GROUP</span> <span class="keyword">BY</span> email</span><br><span class="line">        ) <span class="keyword">AS</span> m</span><br><span class="line">    );</span><br></pre></td></tr></table></figure>

<p>应该注意的是上述解法额外嵌套了一个 SELECT 语句，如果不这么做，会出现错误：You can’t specify target table ‘Person’ for update in FROM clause。以下演示了这种错误解法。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DELETE</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Person</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    id <span class="keyword">NOT</span> <span class="keyword">IN</span> ( </span><br><span class="line">        <span class="keyword">SELECT</span> <span class="built_in">min</span>( id ) <span class="keyword">AS</span> id </span><br><span class="line">        <span class="keyword">FROM</span> Person </span><br><span class="line">        <span class="keyword">GROUP</span> <span class="keyword">BY</span> email </span><br><span class="line">    );</span><br></pre></td></tr></table></figure>

<p>参考：<a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause">pMySQL Error 1093 - Can’t specify target table for update in FROM clause</a></p>
<h3 id="SQL-Schema-5"><a href="#SQL-Schema-5" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><p>与 182 相同。</p>
<h2 id="175-Combine-Two-Tables"><a href="#175-Combine-Two-Tables" class="headerlink" title="175. Combine Two Tables"></a>175. Combine Two Tables</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/combine-two-tables/description/">https://leetcode.com/problems/combine-two-tables/description/</a></p>
<h3 id="Description-6"><a href="#Description-6" class="headerlink" title="Description"></a>Description</h3><p>Person 表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+-------------+---------+</span><br><span class="line">| Column Name | Type    |</span><br><span class="line">+-------------+---------+</span><br><span class="line">| PersonId    | int     |</span><br><span class="line">| FirstName   | varchar |</span><br><span class="line">| LastName    | varchar |</span><br><span class="line">+-------------+---------+</span><br><span class="line">PersonId is the primary key column for this table.</span><br></pre></td></tr></table></figure>

<p>Address 表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+-------------+---------+</span><br><span class="line">| Column Name | Type    |</span><br><span class="line">+-------------+---------+</span><br><span class="line">| AddressId   | int     |</span><br><span class="line">| PersonId    | int     |</span><br><span class="line">| City        | varchar |</span><br><span class="line">| State       | varchar |</span><br><span class="line">+-------------+---------+</span><br><span class="line">AddressId is the primary key column for this table.</span><br></pre></td></tr></table></figure>

<p>查找 FirstName, LastName, City, State 数据，而不管一个用户有没有填地址信息。</p>
<h3 id="Solution-6"><a href="#Solution-6" class="headerlink" title="Solution"></a>Solution</h3><p>涉及到 Person 和 Address 两个表，在对这两个表执行连接操作时，因为要保留 Person 表中的信息，即使在 Address 表中没有关联的信息也要保留。此时可以用左外连接，将 Person 表放在 LEFT JOIN 的左边。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    FirstName,</span><br><span class="line">    LastName,</span><br><span class="line">    City,</span><br><span class="line">    State</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Person P</span><br><span class="line">    <span class="keyword">LEFT</span> <span class="keyword">JOIN</span> Address A</span><br><span class="line">    <span class="keyword">ON</span> P.PersonId <span class="operator">=</span> A.PersonId;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-6"><a href="#SQL-Schema-6" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> Person;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Person ( PersonId <span class="type">INT</span>, FirstName <span class="type">VARCHAR</span> ( <span class="number">255</span> ), LastName <span class="type">VARCHAR</span> ( <span class="number">255</span> ) );</span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> Address;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Address ( AddressId <span class="type">INT</span>, PersonId <span class="type">INT</span>, City <span class="type">VARCHAR</span> ( <span class="number">255</span> ), State <span class="type">VARCHAR</span> ( <span class="number">255</span> ) );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Person ( PersonId, LastName, FirstName )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="string">&#x27;Wang&#x27;</span>, <span class="string">&#x27;Allen&#x27;</span> );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Address ( AddressId, PersonId, City, State )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="number">2</span>, <span class="string">&#x27;New York City&#x27;</span>, <span class="string">&#x27;New York&#x27;</span> );</span><br></pre></td></tr></table></figure>

<h2 id="181-Employees-Earning-More-Than-Their-Managers"><a href="#181-Employees-Earning-More-Than-Their-Managers" class="headerlink" title="181. Employees Earning More Than Their Managers"></a>181. Employees Earning More Than Their Managers</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/employees-earning-more-than-their-managers/description/">https://leetcode.com/problems/employees-earning-more-than-their-managers/description/</a></p>
<h3 id="Description-7"><a href="#Description-7" class="headerlink" title="Description"></a>Description</h3><p>Employee 表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+-------+--------+-----------+</span><br><span class="line">| Id | Name  | Salary | ManagerId |</span><br><span class="line">+----+-------+--------+-----------+</span><br><span class="line">| 1  | Joe   | 70000  | 3         |</span><br><span class="line">| 2  | Henry | 80000  | 4         |</span><br><span class="line">| 3  | Sam   | 60000  | NULL      |</span><br><span class="line">| 4  | Max   | 90000  | NULL      |</span><br><span class="line">+----+-------+--------+-----------+</span><br></pre></td></tr></table></figure>

<p>查找薪资大于其经理薪资的员工信息。</p>
<h3 id="Solution-7"><a href="#Solution-7" class="headerlink" title="Solution"></a>Solution</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    E1.NAME <span class="keyword">AS</span> Employee</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Employee E1</span><br><span class="line">    <span class="keyword">INNER</span> <span class="keyword">JOIN</span> Employee E2</span><br><span class="line">    <span class="keyword">ON</span> E1.ManagerId <span class="operator">=</span> E2.Id</span><br><span class="line">    <span class="keyword">AND</span> E1.Salary <span class="operator">&gt;</span> E2.Salary;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-7"><a href="#SQL-Schema-7" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> Employee;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Employee ( Id <span class="type">INT</span>, NAME <span class="type">VARCHAR</span> ( <span class="number">255</span> ), Salary <span class="type">INT</span>, ManagerId <span class="type">INT</span> );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Employee ( Id, NAME, Salary, ManagerId )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="string">&#x27;Joe&#x27;</span>, <span class="number">70000</span>, <span class="number">3</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="string">&#x27;Henry&#x27;</span>, <span class="number">80000</span>, <span class="number">4</span> ),</span><br><span class="line">    ( <span class="number">3</span>, <span class="string">&#x27;Sam&#x27;</span>, <span class="number">60000</span>, <span class="keyword">NULL</span> ),</span><br><span class="line">    ( <span class="number">4</span>, <span class="string">&#x27;Max&#x27;</span>, <span class="number">90000</span>, <span class="keyword">NULL</span> );</span><br></pre></td></tr></table></figure>

<h2 id="183-Customers-Who-Never-Order"><a href="#183-Customers-Who-Never-Order" class="headerlink" title="183. Customers Who Never Order"></a>183. Customers Who Never Order</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/customers-who-never-order/description/">https://leetcode.com/problems/customers-who-never-order/description/</a></p>
<h3 id="Description-8"><a href="#Description-8" class="headerlink" title="Description"></a>Description</h3><p>Customers 表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+-------+</span><br><span class="line">| Id | Name  |</span><br><span class="line">+----+-------+</span><br><span class="line">| 1  | Joe   |</span><br><span class="line">| 2  | Henry |</span><br><span class="line">| 3  | Sam   |</span><br><span class="line">| 4  | Max   |</span><br><span class="line">+----+-------+</span><br></pre></td></tr></table></figure>

<p>Orders 表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+------------+</span><br><span class="line">| Id | CustomerId |</span><br><span class="line">+----+------------+</span><br><span class="line">| 1  | 3          |</span><br><span class="line">| 2  | 1          |</span><br><span class="line">+----+------------+</span><br></pre></td></tr></table></figure>

<p>查找没有订单的顾客信息：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+-----------+</span><br><span class="line">| Customers |</span><br><span class="line">+-----------+</span><br><span class="line">| Henry     |</span><br><span class="line">| Max       |</span><br><span class="line">+-----------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution-8"><a href="#Solution-8" class="headerlink" title="Solution"></a>Solution</h3><p>左外链接</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    C.Name <span class="keyword">AS</span> Customers</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Customers C</span><br><span class="line">    <span class="keyword">LEFT</span> <span class="keyword">JOIN</span> Orders O</span><br><span class="line">    <span class="keyword">ON</span> C.Id <span class="operator">=</span> O.CustomerId</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    O.CustomerId <span class="keyword">IS</span> <span class="keyword">NULL</span>;</span><br></pre></td></tr></table></figure>

<p>子查询</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    Name <span class="keyword">AS</span> Customers</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Customers</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    Id <span class="keyword">NOT</span> <span class="keyword">IN</span> ( </span><br><span class="line">        <span class="keyword">SELECT</span> CustomerId </span><br><span class="line">        <span class="keyword">FROM</span> Orders </span><br><span class="line">    );</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-8"><a href="#SQL-Schema-8" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> Customers;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Customers ( Id <span class="type">INT</span>, NAME <span class="type">VARCHAR</span> ( <span class="number">255</span> ) );</span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> Orders;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Orders ( Id <span class="type">INT</span>, CustomerId <span class="type">INT</span> );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Customers ( Id, NAME )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="string">&#x27;Joe&#x27;</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="string">&#x27;Henry&#x27;</span> ),</span><br><span class="line">    ( <span class="number">3</span>, <span class="string">&#x27;Sam&#x27;</span> ),</span><br><span class="line">    ( <span class="number">4</span>, <span class="string">&#x27;Max&#x27;</span> );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Orders ( Id, CustomerId )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="number">3</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="number">1</span> );</span><br></pre></td></tr></table></figure>

<h2 id="184-Department-Highest-Salary"><a href="#184-Department-Highest-Salary" class="headerlink" title="184. Department Highest Salary"></a>184. Department Highest Salary</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/department-highest-salary/description/">https://leetcode.com/problems/department-highest-salary/description/</a></p>
<h3 id="Description-9"><a href="#Description-9" class="headerlink" title="Description"></a>Description</h3><p>Employee 表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+-------+--------+--------------+</span><br><span class="line">| Id | Name  | Salary | DepartmentId |</span><br><span class="line">+----+-------+--------+--------------+</span><br><span class="line">| 1  | Joe   | 70000  | 1            |</span><br><span class="line">| 2  | Henry | 80000  | 2            |</span><br><span class="line">| 3  | Sam   | 60000  | 2            |</span><br><span class="line">| 4  | Max   | 90000  | 1            |</span><br><span class="line">+----+-------+--------+--------------+</span><br></pre></td></tr></table></figure>

<p>Department 表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+----------+</span><br><span class="line">| Id | Name     |</span><br><span class="line">+----+----------+</span><br><span class="line">| 1  | IT       |</span><br><span class="line">| 2  | Sales    |</span><br><span class="line">+----+----------+</span><br></pre></td></tr></table></figure>

<p>查找一个 Department 中收入最高者的信息：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+------------+----------+--------+</span><br><span class="line">| Department | Employee | Salary |</span><br><span class="line">+------------+----------+--------+</span><br><span class="line">| IT         | Max      | 90000  |</span><br><span class="line">| Sales      | Henry    | 80000  |</span><br><span class="line">+------------+----------+--------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution-9"><a href="#Solution-9" class="headerlink" title="Solution"></a>Solution</h3><p>创建一个临时表，包含了部门员工的最大薪资。可以对部门进行分组，然后使用 MAX() 汇总函数取得最大薪资。</p>
<p>之后使用连接找到一个部门中薪资等于临时表中最大薪资的员工。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    D.NAME Department,</span><br><span class="line">    E.NAME Employee,</span><br><span class="line">    E.Salary</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Employee E,</span><br><span class="line">    Department D,</span><br><span class="line">    ( <span class="keyword">SELECT</span> DepartmentId, <span class="built_in">MAX</span>( Salary ) Salary </span><br><span class="line">     <span class="keyword">FROM</span> Employee </span><br><span class="line">     <span class="keyword">GROUP</span> <span class="keyword">BY</span> DepartmentId ) M</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    E.DepartmentId <span class="operator">=</span> D.Id</span><br><span class="line">    <span class="keyword">AND</span> E.DepartmentId <span class="operator">=</span> M.DepartmentId</span><br><span class="line">    <span class="keyword">AND</span> E.Salary <span class="operator">=</span> M.Salary;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-9"><a href="#SQL-Schema-9" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> Employee;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Employee ( Id <span class="type">INT</span>, NAME <span class="type">VARCHAR</span> ( <span class="number">255</span> ), Salary <span class="type">INT</span>, DepartmentId <span class="type">INT</span> );</span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> Department;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Department ( Id <span class="type">INT</span>, NAME <span class="type">VARCHAR</span> ( <span class="number">255</span> ) );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Employee ( Id, NAME, Salary, DepartmentId )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="string">&#x27;Joe&#x27;</span>, <span class="number">70000</span>, <span class="number">1</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="string">&#x27;Henry&#x27;</span>, <span class="number">80000</span>, <span class="number">2</span> ),</span><br><span class="line">    ( <span class="number">3</span>, <span class="string">&#x27;Sam&#x27;</span>, <span class="number">60000</span>, <span class="number">2</span> ),</span><br><span class="line">    ( <span class="number">4</span>, <span class="string">&#x27;Max&#x27;</span>, <span class="number">90000</span>, <span class="number">1</span> );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Department ( Id, NAME )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="string">&#x27;IT&#x27;</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="string">&#x27;Sales&#x27;</span> );</span><br></pre></td></tr></table></figure>


<h2 id="176-Second-Highest-Salary"><a href="#176-Second-Highest-Salary" class="headerlink" title="176. Second Highest Salary"></a>176. Second Highest Salary</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/second-highest-salary/description/">https://leetcode.com/problems/second-highest-salary/description/</a></p>
<h3 id="Description-10"><a href="#Description-10" class="headerlink" title="Description"></a>Description</h3><figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+--------+</span><br><span class="line">| Id | Salary |</span><br><span class="line">+----+--------+</span><br><span class="line">| 1  | 100    |</span><br><span class="line">| 2  | 200    |</span><br><span class="line">| 3  | 300    |</span><br><span class="line">+----+--------+</span><br></pre></td></tr></table></figure>

<p>查找工资第二高的员工。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+---------------------+</span><br><span class="line">| SecondHighestSalary |</span><br><span class="line">+---------------------+</span><br><span class="line">| 200                 |</span><br><span class="line">+---------------------+</span><br></pre></td></tr></table></figure>

<p>没有找到返回 null 而不是不返回数据。</p>
<h3 id="Solution-10"><a href="#Solution-10" class="headerlink" title="Solution"></a>Solution</h3><p>为了在没有查找到数据时返回 null，需要在查询结果外面再套一层 SELECT。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    ( <span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> Salary </span><br><span class="line">     <span class="keyword">FROM</span> Employee </span><br><span class="line">     <span class="keyword">ORDER</span> <span class="keyword">BY</span> Salary <span class="keyword">DESC</span> </span><br><span class="line">     LIMIT <span class="number">1</span>, <span class="number">1</span> ) SecondHighestSalary;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-10"><a href="#SQL-Schema-10" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> Employee;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Employee ( Id <span class="type">INT</span>, Salary <span class="type">INT</span> );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Employee ( Id, Salary )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="number">100</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="number">200</span> ),</span><br><span class="line">    ( <span class="number">3</span>, <span class="number">300</span> );</span><br></pre></td></tr></table></figure>

<h2 id="177-Nth-Highest-Salary"><a href="#177-Nth-Highest-Salary" class="headerlink" title="177. Nth Highest Salary"></a>177. Nth Highest Salary</h2><h3 id="Description-11"><a href="#Description-11" class="headerlink" title="Description"></a>Description</h3><p>查找工资第 N 高的员工。</p>
<h3 id="Solution-11"><a href="#Solution-11" class="headerlink" title="Solution"></a>Solution</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">FUNCTION</span> getNthHighestSalary ( N <span class="type">INT</span> ) <span class="keyword">RETURNS</span> <span class="type">INT</span> <span class="keyword">BEGIN</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">SET</span> N <span class="operator">=</span> N <span class="operator">-</span> <span class="number">1</span>;</span><br><span class="line"><span class="keyword">RETURN</span> ( </span><br><span class="line">    <span class="keyword">SELECT</span> ( </span><br><span class="line">        <span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> Salary </span><br><span class="line">        <span class="keyword">FROM</span> Employee </span><br><span class="line">        <span class="keyword">ORDER</span> <span class="keyword">BY</span> Salary <span class="keyword">DESC</span> </span><br><span class="line">        LIMIT N, <span class="number">1</span> </span><br><span class="line">    ) </span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">END</span></span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-11"><a href="#SQL-Schema-11" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><p>同 176。</p>
<h2 id="178-Rank-Scores"><a href="#178-Rank-Scores" class="headerlink" title="178. Rank Scores"></a>178. Rank Scores</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/rank-scores/description/">https://leetcode.com/problems/rank-scores/description/</a></p>
<h3 id="Description-12"><a href="#Description-12" class="headerlink" title="Description"></a>Description</h3><p>得分表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+-------+</span><br><span class="line">| Id | Score |</span><br><span class="line">+----+-------+</span><br><span class="line">| 1  | 3.50  |</span><br><span class="line">| 2  | 3.65  |</span><br><span class="line">| 3  | 4.00  |</span><br><span class="line">| 4  | 3.85  |</span><br><span class="line">| 5  | 4.00  |</span><br><span class="line">| 6  | 3.65  |</span><br><span class="line">+----+-------+</span><br></pre></td></tr></table></figure>

<p>将得分排序，并统计排名。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+-------+------+</span><br><span class="line">| Score | Rank |</span><br><span class="line">+-------+------+</span><br><span class="line">| 4.00  | 1    |</span><br><span class="line">| 4.00  | 1    |</span><br><span class="line">| 3.85  | 2    |</span><br><span class="line">| 3.65  | 3    |</span><br><span class="line">| 3.65  | 3    |</span><br><span class="line">| 3.50  | 4    |</span><br><span class="line">+-------+------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution-12"><a href="#Solution-12" class="headerlink" title="Solution"></a>Solution</h3><p>要统计某个 score 的排名，只要统计大于等于该 score 的 score 数量。</p>
<table>
<thead>
<tr>
<th align="center">Id</th>
<th align="center">score</th>
<th align="center">大于等于该 score 的 score 数量</th>
<th align="center">排名</th>
</tr>
</thead>
<tbody><tr>
<td align="center">1</td>
<td align="center">4.1</td>
<td align="center">3</td>
<td align="center">3</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">4.2</td>
<td align="center">2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">4.3</td>
<td align="center">1</td>
<td align="center">1</td>
</tr>
</tbody></table>
<p>使用连接操作找到某个 score 对应的大于等于其值的记录：</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">	<span class="operator">*</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Scores S1</span><br><span class="line">    <span class="keyword">INNER</span> <span class="keyword">JOIN</span> Scores S2</span><br><span class="line">    <span class="keyword">ON</span> S1.score <span class="operator">&lt;=</span> S2.score</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span></span><br><span class="line">    S1.score <span class="keyword">DESC</span>, S1.Id;</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th align="center">S1.Id</th>
<th align="center">S1.score</th>
<th align="center">S2.Id</th>
<th align="center">S2.score</th>
</tr>
</thead>
<tbody><tr>
<td align="center">3</td>
<td align="center">4.3</td>
<td align="center">3</td>
<td align="center">4.3</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">4.2</td>
<td align="center">2</td>
<td align="center">4.2</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">4.2</td>
<td align="center">3</td>
<td align="center">4.3</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">4.1</td>
<td align="center">1</td>
<td align="center">4.1</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">4.1</td>
<td align="center">2</td>
<td align="center">4.2</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">4.1</td>
<td align="center">3</td>
<td align="center">4.3</td>
</tr>
</tbody></table>
<p>可以看到每个 S1.score 都有对应好几条记录，我们再进行分组，并统计每个分组的数量作为 ‘Rank’</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    S1.score <span class="string">&#x27;Score&#x27;</span>,</span><br><span class="line">    <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="string">&#x27;Rank&#x27;</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Scores S1</span><br><span class="line">    <span class="keyword">INNER</span> <span class="keyword">JOIN</span> Scores S2</span><br><span class="line">    <span class="keyword">ON</span> S1.score <span class="operator">&lt;=</span> S2.score</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">    S1.id, S1.score</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span></span><br><span class="line">    S1.score <span class="keyword">DESC</span>, S1.Id;</span><br></pre></td></tr></table></figure>

<table>
<thead>
<tr>
<th align="center">score</th>
<th align="center">Rank</th>
</tr>
</thead>
<tbody><tr>
<td align="center">4.3</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">4.2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">4.1</td>
<td align="center">3</td>
</tr>
</tbody></table>
<p>上面的解法看似没问题，但是对于以下数据，它却得到了错误的结果：</p>
<table>
<thead>
<tr>
<th align="center">Id</th>
<th align="center">score</th>
</tr>
</thead>
<tbody><tr>
<td align="center">1</td>
<td align="center">4.1</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">4.2</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">4.2</td>
</tr>
</tbody></table>
<table>
<thead>
<tr>
<th align="center">score</th>
<th align="center">Rank</th>
</tr>
</thead>
<tbody><tr>
<td align="center">4.2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">4.2</td>
<td align="center">2</td>
</tr>
<tr>
<td align="center">4.1</td>
<td align="center">3</td>
</tr>
</tbody></table>
<p>而我们希望的结果为：</p>
<table>
<thead>
<tr>
<th align="center">score</th>
<th align="center">Rank</th>
</tr>
</thead>
<tbody><tr>
<td align="center">4.2</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">4.2</td>
<td align="center">1</td>
</tr>
<tr>
<td align="center">4.1</td>
<td align="center">2</td>
</tr>
</tbody></table>
<p>连接情况如下：</p>
<table>
<thead>
<tr>
<th align="center">S1.Id</th>
<th align="center">S1.score</th>
<th align="center">S2.Id</th>
<th align="center">S2.score</th>
</tr>
</thead>
<tbody><tr>
<td align="center">2</td>
<td align="center">4.2</td>
<td align="center">3</td>
<td align="center">4.2</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">4.2</td>
<td align="center">2</td>
<td align="center">4.2</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">4.2</td>
<td align="center">3</td>
<td align="center">4.2</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">4.2</td>
<td align="center">2</td>
<td align="center">4.1</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">4.1</td>
<td align="center">3</td>
<td align="center">4.2</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">4.1</td>
<td align="center">2</td>
<td align="center">4.2</td>
</tr>
<tr>
<td align="center">1</td>
<td align="center">4.1</td>
<td align="center">1</td>
<td align="center">4.1</td>
</tr>
</tbody></table>
<p>我们想要的结果是，把分数相同的放在同一个排名，并且相同分数只占一个位置，例如上面的分数，Id=2 和 Id=3 的记录都有相同的分数，并且最高，他们并列第一。而 Id=1 的记录应该排第二名，而不是第三名。所以在进行 COUNT 计数统计时，我们需要使用 COUNT( DISTINCT S2.score ) 从而只统计一次相同的分数。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    S1.score <span class="string">&#x27;Score&#x27;</span>,</span><br><span class="line">    <span class="built_in">COUNT</span>( <span class="keyword">DISTINCT</span> S2.score ) <span class="string">&#x27;Rank&#x27;</span></span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Scores S1</span><br><span class="line">    <span class="keyword">INNER</span> <span class="keyword">JOIN</span> Scores S2</span><br><span class="line">    <span class="keyword">ON</span> S1.score <span class="operator">&lt;=</span> S2.score</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span></span><br><span class="line">    S1.id, S1.score</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span></span><br><span class="line">    S1.score <span class="keyword">DESC</span>;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-12"><a href="#SQL-Schema-12" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> Scores;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> Scores ( Id <span class="type">INT</span>, Score <span class="type">DECIMAL</span> ( <span class="number">3</span>, <span class="number">2</span> ) );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> Scores ( Id, Score )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="number">4.1</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="number">4.1</span> ),</span><br><span class="line">    ( <span class="number">3</span>, <span class="number">4.2</span> ),</span><br><span class="line">    ( <span class="number">4</span>, <span class="number">4.2</span> ),</span><br><span class="line">    ( <span class="number">5</span>, <span class="number">4.3</span> ),</span><br><span class="line">    ( <span class="number">6</span>, <span class="number">4.3</span> );</span><br></pre></td></tr></table></figure>

<h2 id="180-Consecutive-Numbers"><a href="#180-Consecutive-Numbers" class="headerlink" title="180. Consecutive Numbers"></a>180. Consecutive Numbers</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/consecutive-numbers/description/">https://leetcode.com/problems/consecutive-numbers/description/</a></p>
<h3 id="Description-13"><a href="#Description-13" class="headerlink" title="Description"></a>Description</h3><p>数字表：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+----+-----+</span><br><span class="line">| Id | Num |</span><br><span class="line">+----+-----+</span><br><span class="line">| 1  |  1  |</span><br><span class="line">| 2  |  1  |</span><br><span class="line">| 3  |  1  |</span><br><span class="line">| 4  |  2  |</span><br><span class="line">| 5  |  1  |</span><br><span class="line">| 6  |  2  |</span><br><span class="line">| 7  |  2  |</span><br><span class="line">+----+-----+</span><br></pre></td></tr></table></figure>

<p>查找连续出现三次的数字。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+-----------------+</span><br><span class="line">| ConsecutiveNums |</span><br><span class="line">+-----------------+</span><br><span class="line">| 1               |</span><br><span class="line">+-----------------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution-13"><a href="#Solution-13" class="headerlink" title="Solution"></a>Solution</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    <span class="keyword">DISTINCT</span> L1.num ConsecutiveNums</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    Logs L1,</span><br><span class="line">    Logs L2,</span><br><span class="line">    Logs L3</span><br><span class="line"><span class="keyword">WHERE</span> L1.id <span class="operator">=</span> l2.id <span class="operator">-</span> <span class="number">1</span></span><br><span class="line">    <span class="keyword">AND</span> L2.id <span class="operator">=</span> L3.id <span class="operator">-</span> <span class="number">1</span></span><br><span class="line">    <span class="keyword">AND</span> L1.num <span class="operator">=</span> L2.num</span><br><span class="line">    <span class="keyword">AND</span> l2.num <span class="operator">=</span> l3.num;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-13"><a href="#SQL-Schema-13" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> LOGS;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> LOGS ( Id <span class="type">INT</span>, Num <span class="type">INT</span> );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> LOGS ( Id, Num )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="number">1</span>, <span class="number">1</span> ),</span><br><span class="line">    ( <span class="number">2</span>, <span class="number">1</span> ),</span><br><span class="line">    ( <span class="number">3</span>, <span class="number">1</span> ),</span><br><span class="line">    ( <span class="number">4</span>, <span class="number">2</span> ),</span><br><span class="line">    ( <span class="number">5</span>, <span class="number">1</span> ),</span><br><span class="line">    ( <span class="number">6</span>, <span class="number">2</span> ),</span><br><span class="line">    ( <span class="number">7</span>, <span class="number">2</span> );</span><br></pre></td></tr></table></figure>

<h2 id="626-Exchange-Seats"><a href="#626-Exchange-Seats" class="headerlink" title="626. Exchange Seats"></a>626. Exchange Seats</h2><p><a target="_blank" rel="noopener" href="https://leetcode.com/problems/exchange-seats/description/">https://leetcode.com/problems/exchange-seats/description/</a></p>
<h3 id="Description-14"><a href="#Description-14" class="headerlink" title="Description"></a>Description</h3><p>seat 表存储着座位对应的学生。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+---------+---------+</span><br><span class="line">|    id   | student |</span><br><span class="line">+---------+---------+</span><br><span class="line">|    1    | Abbot   |</span><br><span class="line">|    2    | Doris   |</span><br><span class="line">|    3    | Emerson |</span><br><span class="line">|    4    | Green   |</span><br><span class="line">|    5    | Jeames  |</span><br><span class="line">+---------+---------+</span><br></pre></td></tr></table></figure>

<p>要求交换相邻座位的两个学生，如果最后一个座位是奇数，那么不交换这个座位上的学生。</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line">+---------+---------+</span><br><span class="line">|    id   | student |</span><br><span class="line">+---------+---------+</span><br><span class="line">|    1    | Doris   |</span><br><span class="line">|    2    | Abbot   |</span><br><span class="line">|    3    | Green   |</span><br><span class="line">|    4    | Emerson |</span><br><span class="line">|    5    | Jeames  |</span><br><span class="line">+---------+---------+</span><br></pre></td></tr></table></figure>

<h3 id="Solution-14"><a href="#Solution-14" class="headerlink" title="Solution"></a>Solution</h3><p>使用多个 union。</p>
<figure class="highlight sql"><table><tr><td class="code"><pre><span class="line">## 处理偶数 id，让 id 减 <span class="number">1</span></span><br><span class="line">## 例如 <span class="number">2</span>,<span class="number">4</span>,<span class="number">6</span>,... 变成 <span class="number">1</span>,<span class="number">3</span>,<span class="number">5</span>,...</span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    s1.id <span class="operator">-</span> <span class="number">1</span> <span class="keyword">AS</span> id,</span><br><span class="line">    s1.student</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    seat s1</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    s1.id MOD <span class="number">2</span> <span class="operator">=</span> <span class="number">0</span> <span class="keyword">UNION</span></span><br><span class="line">## 处理奇数 id，让 id 加 <span class="number">1</span>。但是如果最大的 id 为奇数，则不做处理</span><br><span class="line">## 例如 <span class="number">1</span>,<span class="number">3</span>,<span class="number">5</span>,... 变成 <span class="number">2</span>,<span class="number">4</span>,<span class="number">6</span>,...</span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    s2.id <span class="operator">+</span> <span class="number">1</span> <span class="keyword">AS</span> id,</span><br><span class="line">    s2.student</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    seat s2</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    s2.id MOD <span class="number">2</span> <span class="operator">=</span> <span class="number">1</span></span><br><span class="line">    <span class="keyword">AND</span> s2.id <span class="operator">!=</span> ( <span class="keyword">SELECT</span> <span class="built_in">max</span>( s3.id ) <span class="keyword">FROM</span> seat s3 ) <span class="keyword">UNION</span></span><br><span class="line">## 如果最大的 id 为奇数，单独取出这个数</span><br><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">    s4.id <span class="keyword">AS</span> id,</span><br><span class="line">    s4.student</span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">    seat s4</span><br><span class="line"><span class="keyword">WHERE</span></span><br><span class="line">    s4.id MOD <span class="number">2</span> <span class="operator">=</span> <span class="number">1</span></span><br><span class="line">    <span class="keyword">AND</span> s4.id <span class="operator">=</span> ( <span class="keyword">SELECT</span> <span class="built_in">max</span>( s5.id ) <span class="keyword">FROM</span> seat s5 )</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span></span><br><span class="line">    id;</span><br></pre></td></tr></table></figure>

<h3 id="SQL-Schema-14"><a href="#SQL-Schema-14" class="headerlink" title="SQL Schema"></a>SQL Schema</h3><figure class="highlight sql"><table><tr><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span></span><br><span class="line">IF</span><br><span class="line">    <span class="keyword">EXISTS</span> seat;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> seat ( id <span class="type">INT</span>, student <span class="type">VARCHAR</span> ( <span class="number">255</span> ) );</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> seat ( id, student )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">    ( <span class="string">&#x27;1&#x27;</span>, <span class="string">&#x27;Abbot&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;2&#x27;</span>, <span class="string">&#x27;Doris&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;3&#x27;</span>, <span class="string">&#x27;Emerson&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;4&#x27;</span>, <span class="string">&#x27;Green&#x27;</span> ),</span><br><span class="line">    ( <span class="string">&#x27;5&#x27;</span>, <span class="string">&#x27;Jeames&#x27;</span> );</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Zhang Shuo</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://zhang-shuo-fr.gitee.io/hexo3/2021/12/06/notes/SQL%20%E7%BB%83%E4%B9%A0/">https://zhang-shuo-fr.gitee.io/hexo3/2021/12/06/notes/SQL%20%E7%BB%83%E4%B9%A0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://zhang-shuo-fr.gitee.io/hexo3" target="_blank">Zhang Shuo'blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/hexo3/tags/SQL%E7%BB%83%E4%B9%A0/">SQL练习</a></div><div class="post_share"><div class="social-share" data-image="/hexo3/img/13.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button button--animated"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/hexo3/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="/hexo3/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/hexo3/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src= "" data-lazy-src="/hexo3/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/hexo3/2021/12/06/notes/SQL%20%E8%AF%AD%E6%B3%95/"><img class="prev-cover" src= "" data-lazy-src="/hexo3/img/13.jpg" onerror="onerror=null;src='/hexo3/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">SQL语法</div></div></a></div><div class="next-post pull-right"><a href="/hexo3/2021/12/06/notes/Leetcode%20%E9%A2%98%E8%A7%A3%20-%20%E6%A0%91/"><img class="next-cover" src= "" data-lazy-src="/hexo3/img/13.jpg" onerror="onerror=null;src='/hexo3/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Leetcode 题解 - 树</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#SQL-%E7%BB%83%E4%B9%A0"><span class="toc-number">1.</span> <span class="toc-text">SQL 练习</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#595-Big-Countries"><span class="toc-number">1.1.</span> <span class="toc-text">595. Big Countries</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description"><span class="toc-number">1.1.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution"><span class="toc-number">1.1.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema"><span class="toc-number">1.1.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#627-Swap-Salary"><span class="toc-number">1.2.</span> <span class="toc-text">627. Swap Salary</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-1"><span class="toc-number">1.2.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-1"><span class="toc-number">1.2.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-1"><span class="toc-number">1.2.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#620-Not-Boring-Movies"><span class="toc-number">1.3.</span> <span class="toc-text">620. Not Boring Movies</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-2"><span class="toc-number">1.3.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-2"><span class="toc-number">1.3.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-2"><span class="toc-number">1.3.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#596-Classes-More-Than-5-Students"><span class="toc-number">1.4.</span> <span class="toc-text">596. Classes More Than 5 Students</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-3"><span class="toc-number">1.4.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-3"><span class="toc-number">1.4.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-3"><span class="toc-number">1.4.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#182-Duplicate-Emails"><span class="toc-number">1.5.</span> <span class="toc-text">182. Duplicate Emails</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-4"><span class="toc-number">1.5.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-4"><span class="toc-number">1.5.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-4"><span class="toc-number">1.5.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#196-Delete-Duplicate-Emails"><span class="toc-number">1.6.</span> <span class="toc-text">196. Delete Duplicate Emails</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-5"><span class="toc-number">1.6.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-5"><span class="toc-number">1.6.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-5"><span class="toc-number">1.6.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#175-Combine-Two-Tables"><span class="toc-number">1.7.</span> <span class="toc-text">175. Combine Two Tables</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-6"><span class="toc-number">1.7.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-6"><span class="toc-number">1.7.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-6"><span class="toc-number">1.7.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#181-Employees-Earning-More-Than-Their-Managers"><span class="toc-number">1.8.</span> <span class="toc-text">181. Employees Earning More Than Their Managers</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-7"><span class="toc-number">1.8.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-7"><span class="toc-number">1.8.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-7"><span class="toc-number">1.8.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#183-Customers-Who-Never-Order"><span class="toc-number">1.9.</span> <span class="toc-text">183. Customers Who Never Order</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-8"><span class="toc-number">1.9.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-8"><span class="toc-number">1.9.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-8"><span class="toc-number">1.9.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#184-Department-Highest-Salary"><span class="toc-number">1.10.</span> <span class="toc-text">184. Department Highest Salary</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-9"><span class="toc-number">1.10.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-9"><span class="toc-number">1.10.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-9"><span class="toc-number">1.10.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#176-Second-Highest-Salary"><span class="toc-number">1.11.</span> <span class="toc-text">176. Second Highest Salary</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-10"><span class="toc-number">1.11.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-10"><span class="toc-number">1.11.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-10"><span class="toc-number">1.11.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#177-Nth-Highest-Salary"><span class="toc-number">1.12.</span> <span class="toc-text">177. Nth Highest Salary</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-11"><span class="toc-number">1.12.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-11"><span class="toc-number">1.12.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-11"><span class="toc-number">1.12.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#178-Rank-Scores"><span class="toc-number">1.13.</span> <span class="toc-text">178. Rank Scores</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-12"><span class="toc-number">1.13.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-12"><span class="toc-number">1.13.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-12"><span class="toc-number">1.13.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#180-Consecutive-Numbers"><span class="toc-number">1.14.</span> <span class="toc-text">180. Consecutive Numbers</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-13"><span class="toc-number">1.14.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-13"><span class="toc-number">1.14.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-13"><span class="toc-number">1.14.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#626-Exchange-Seats"><span class="toc-number">1.15.</span> <span class="toc-text">626. Exchange Seats</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Description-14"><span class="toc-number">1.15.1.</span> <span class="toc-text">Description</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Solution-14"><span class="toc-number">1.15.2.</span> <span class="toc-text">Solution</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SQL-Schema-14"><span class="toc-number">1.15.3.</span> <span class="toc-text">SQL Schema</span></a></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('/hexo3/img/13.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Zhang Shuo</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Hi, welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">簡</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/hexo3/js/utils.js"></script><script src="/hexo3/js/main.js"></script><script src="/hexo3/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="/hexo3/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><div class="aplayer no-destroy" data-id="000PeZCQ1i4XVs" data-server="tencent" data-type="artist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay="true" muted></div><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = true;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.removeEventListener('scroll', window.tocScrollFn)
  window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>